#include "PowerFlowCalculation.h"

#include <iostream>
#include "readcsv.h"

using namespace std;

RE_IEEE14 LoadData(){
    RE_IEEE14 ri;
    int k;
    //branch2 -> Normal_Branch
    CSVdata branch2_data=read_from_csv_double("Input_Data(1-24)_branch2.csv");
    int connect_num=0;
    for(int i=0;i<branch2_data.row;i++){
        if(branch2_data.lines[i].ele[7]==1){
            connect_num++;
        }
    }
    k=0;
    ri.Normal_Branch=gsl_matrix_alloc(connect_num,5);
    for(int i=0;i<branch2_data.row;i++){
        if(branch2_data.lines[i].ele[7]==1){
            for(int j=0;j<4;j++){
                gsl_matrix_set(ri.Normal_Branch,k,j,branch2_data.lines[i].ele[j]);
            }
            k++;
        }
        
    }

    PRINT_MATRIX(ri.Normal_Branch);

    //bus1(Type==1) -> PQ_Node
    CSVdata bus1_data=read_from_csv_double("Input_Data(1-24)_bus1.csv");
    int type1num=0;
    for(int i=0;i<bus1_data.row;i++){
        if(bus1_data.lines[i].ele[1]==1){
            type1num++;
        }
    }
    ri.PQ_Node=gsl_matrix_alloc(type1num,7);
    k=0;
    double P_Sum=0,Q_Sum=0;
    for(int i=0;i<bus1_data.row;i++){
        if(bus1_data.lines[i].ele[1]==1){
            gsl_matrix_set(ri.PQ_Node,k,0,bus1_data.lines[i].ele[0]);
            gsl_matrix_set(ri.PQ_Node,k,1,bus1_data.lines[i].ele[2]);
            P_Sum+=bus1_data.lines[i].ele[2];
            gsl_matrix_set(ri.PQ_Node,k,2,bus1_data.lines[i].ele[2]*0.8);
            Q_Sum+=bus1_data.lines[i].ele[2]*0.8;
            gsl_matrix_set(ri.PQ_Node,k,3,1);
            gsl_matrix_set(ri.PQ_Node,k,4,0);
            gsl_matrix_set(ri.PQ_Node,k,5,0);
            gsl_matrix_set(ri.PQ_Node,k,6,0.9);
            k++;
        }

        
    }
    //PRINT_MATRIX(ri.PQ_Node)



    //bus1(Type==3) -> swingNode
    int type3num=0;
    for(int i=0;i<bus1_data.row;i++){
        if(bus1_data.lines[i].ele[1]==3){
            type3num++;
        }
    }
    ri.Swing_Node=gsl_matrix_alloc(type3num,7);
    k=0;
    for(int i=0;i<bus1_data.row;i++){
        if(bus1_data.lines[i].ele[1]==3){
            gsl_matrix_set(ri.Swing_Node,k,0,bus1_data.lines[i].ele[0]);
            gsl_matrix_set(ri.Swing_Node,k,1,1);
            gsl_matrix_set(ri.Swing_Node,k,2,0);
            gsl_matrix_set(ri.Swing_Node,k,3,0);
            gsl_matrix_set(ri.Swing_Node,k,4,0);
            gsl_matrix_set(ri.Swing_Node,k,5,P_Sum);
            gsl_matrix_set(ri.Swing_Node,k,6,Q_Sum);
            k++;
        }

        
    }
    return ri;
}



